const http = require('http');
const fs = require('fs');
const querystring = require('querystring');
const urlLib = require('url');

var server = http.createServer(function (req, res) {
    //解析get请求数据
    var obj = urlLib.parse(req.url, true);
    var url = obj.pathname;
    var GET = obj.query;

    //解析post请求数据
    var str = '';
    req.on('data', function (data) {
        str += data;
    });

    req.on('end', function () {
        var POST = querystring.parse(str);
        console.log(url, GET, POST);


        //解析访问文件
        var file_name = './www/' + url;
        fs.readFile(file_name, function (err, data) {
            if (err) {
                res.write('404');
            } else {
                res.write(data);
            }
            res.end();
        });
    });


});

server.listen(8080);